package com.example.admin.midtermpro41;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;


/*这是专门用来添加事件监听器的类*/
public class eventListerner {
    /*给主页面英雄列表添加检测事件*/
    public static void initHeroAdapterEventLister(QuickAdapter heroAdapter){
        heroAdapter.setOnItemClickListener(new QuickAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position, int viewId) {
                Log.i("debug",MainActivity.heroList[0].get(position).heroName+" "+viewId+ " onClick");
            }

            @Override
            public void onLongClick(int position,int viewId) {
                MainActivity.heroAdapterBuilder.setMessage("确定删除"+ MainActivity.heroList[0].get(position).heroName+"?")
                        .setTitle("删除");
                AlertDialog dialog = MainActivity.heroAdapterBuilder.create();
                MainActivity.OnDeletingItemPosition=position;
                dialog.show();
            }
        });
        MainActivity.heroAdapterBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {//删除确认框
                // User clicked OK button
                Toast.makeText(MainActivity.context, MainActivity.heroList[0].get(MainActivity.OnDeletingItemPosition).heroName+"被删除", Toast.LENGTH_SHORT).show();
                MainActivity.heroList[0].remove(MainActivity.OnDeletingItemPosition);
                MainActivity.heroAdapter.notifyDataSetChanged();
            }
        });
        MainActivity.heroAdapterBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            }
        });
    }

    /*给搜索列表增加点击事件*/
    public static void initSearchAdapter(QuickAdapter searchAdapter){
        searchAdapter.setOnItemClickListener(new QuickAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position,int viewId) {
                Log.i("debug",MainActivity.searchList[0].get(position)+" "+viewId+" onClick");
                if(viewId==R.id.isFoundInHeroList&&!hero.searchHeroIsInHeroListOrNot(MainActivity.searchList[0].get(position))){
                    //+号被点击且不在主页面，要将对应的英雄加入主页面列表
                    MainActivity.OnAddingItenPosition=position;
                    MainActivity.searchAdapterBuilder.setMessage("确定添加"+MainActivity.searchList[0].get(position)+"?").setTitle("添加");
                    AlertDialog dialog=MainActivity.searchAdapterBuilder.create();
                    dialog.show();
                }
            }
            @Override
            public void onLongClick(int position, int viewId) {
            }
        });
        MainActivity.searchAdapterBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                MainActivity.heroList[0].add(hero.getHeroByName(MainActivity.searchList[0].get(MainActivity.OnAddingItenPosition)));
                MainActivity.heroAdapter.notifyDataSetChanged();
                Log.i("debug",MainActivity.searchList[0].get(MainActivity.OnAddingItenPosition)+" add");
            }
        });
        MainActivity.searchAdapterBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });

    }

    /*搜索框关闭动作监听器*/
    public static void initOnCloseListener(SearchView searchView){
        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                Log.i("debug","onClose");
                MainActivity.searchRecyclerView.setVisibility(View.INVISIBLE);
                MainActivity.heroRecyclerView.setVisibility(View.VISIBLE);
                return false;
            }
        });
    }

    /*给搜索输入框增加事件监听器*/
    public static void initQueryTextListener(SearchView searchView){
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {//点击提交事件
                Log.i("debug","onQueryTextSubmit");
                return true;
            }

            @Override
            public boolean onQueryTextChange(String s) {//输入字符
                MainActivity.searchRecyclerView.setVisibility(View.VISIBLE);
                MainActivity.heroRecyclerView.setVisibility(View.INVISIBLE);
                /*利用模糊搜索得到搜索答案列表*/
                String[] searchResult=searchFunction.queryHero(s);
                int size=MainActivity.searchList[0].size();
                for(int i=size-1;i>=0;i--){//从大到小删除
                    MainActivity.searchList[0].remove(i);
                }
                for(int i=0;i<searchResult.length;i++){
                    MainActivity.searchList[0].add(searchResult[i]);
                }
                MainActivity.searchAdapter.notifyDataSetChanged();
                return true;
            }
        });
    }


}
